<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/30
 * Time: 9:12
 */

namespace plugin\admin\app\logic\user;


use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\font\FontModel;
use plugin\admin\app\model\image\ImageCommandPlanModel;
use plugin\admin\app\model\image\ImageModelModel;
use plugin\admin\app\model\novel\NovelChapterModel;
use plugin\admin\app\model\novel\NovelChapterPartModel;
use plugin\admin\app\model\novel\NovelSpreadModel;
use plugin\admin\app\model\plan\BackgroundMusicModel;
use plugin\admin\app\model\tencent\TimbreModel;
use plugin\admin\app\model\user\UserPromotionArticleModel;
use plugin\admin\app\model\user\UserPromotionOverseaModel;
use plugin\admin\app\model\user\UserPromotionPlanModel;
use plugin\admin\app\model\user\UserPromotionVideoModel;
use support\Request;
use support\Response;

class PromotionPlanLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new UserPromotionPlanModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','created_at');
            $request->inputSet('order','desc');
        }

        //$request->inputSet('isdel','0');
        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
        $novelMap = $this->getListMapById(NovelSpreadModel::getInstance(),$items,'novel_sn','novel_sn');
        $chapterMap = $this->getListMapById(NovelChapterModel::getInstance(),$items,'chapterid','id');
        $articleMap = $this->getListMapById(UserPromotionArticleModel::getInstance(),$items,'articleid','id');
        $videoMap = $this->getListMapById(UserPromotionVideoModel::getInstance(),$items,'videoid','id');
        $musicMap = $this->getListMapById(BackgroundMusicModel::getInstance(),$items,'musicid','id');
        $timbreMap = $this->getListMapById(\plugin\admin\app\model\moyin\TimbreModel::getInstance(),$items,'timbreid','id');
        $fontMap = $this->getListMapById(FontModel::getInstance(),$items,'fontid','id');
        $modelMap = $this->getListMapById(ImageCommandPlanModel::getInstance(),$items,'templateid','id');
        $overseaMap = $this->getListMapById(UserPromotionOverseaModel::getInstance(),$items,'overseaid','id');
        foreach($items as &$val){
            if($val['plan_type']==1){
                $val['title'] = isset($novelMap[$val['novel_sn']])?$novelMap[$val['novel_sn']]['title']:$val['novel_sn'];
                $val['content'] = isset($chapterMap[$val['chapterid']])?$chapterMap[$val['chapterid']]['content']:$val['chapterid'];
            }elseif($val['plan_type']==2){
                $val['title'] = '';
                $val['content'] = isset($articleMap[$val['articleid']])?$articleMap[$val['articleid']]['content']:'';
            }elseif($val['plan_type']==3){
                $val['title'] = '';
                $val['content'] = isset($videoMap[$val['videoid']])?$videoMap[$val['videoid']]['content']:'';
            }elseif($val['plan_type']==4){
                if($val['spread_type']==3){
                    $val['title'] = isset($novelMap[$val['novel_sn']])?$novelMap[$val['novel_sn']]['title']:$val['novel_sn'];
                    $val['content'] = isset($overseaMap[$val['overseaid']])?$overseaMap[$val['overseaid']]['content']:$val['overseaid'];
                }else{
                    $val['title'] = '';
                    $val['content'] = isset($overseaMap[$val['overseaid']])?$overseaMap[$val['overseaid']]['content']:$val['overseaid'];
                }
            }

            $val['musicname'] = isset($musicMap[$val['musicid']])?$musicMap[$val['musicid']]['name']:$val['musicid'];
            $val['music_url'] = isset($musicMap[$val['musicid']])?$musicMap[$val['musicid']]['music_url']:'';

            $val['fontname'] = isset($fontMap[$val['fontid']])?$fontMap[$val['fontid']]['name']:$val['fontid'];
            $val['modelname'] = isset($modelMap[$val['templateid']])?$modelMap[$val['templateid']]['name']:$val['templateid'];

            $val['timbrename'] = isset($timbreMap[$val['timbreid']])?
                $timbreMap[$val['timbreid']]['name'].'('.$timbreMap[$val['timbreid']]['scenes'].')':$val['timbreid'];

            if(!empty($val['finish_time']) && !empty($val['queue_time'])){
                $val['finish_tts'] = round(strtotime($val['finish_time'])-strtotime($val['queue_time']));
            }else{
                $val['finish_tts'] = 0;
            }

        }
        return parent::formatNormal($items, $total);
    }

    public function example(Request $request): Response
    {
        return parent::update($request);
    }

}